package com.langsin.jiangjinheng;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.jiangjinheng.mapper.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    SqlSession sqlSession = null;
    @Test
    public void testUseGoodsLazy() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());
        try{Thread.sleep(5000);}catch (Exception e){e.printStackTrace();}

        for (Orders orders: user.getOrdersList()) {
            System.out.println("订单id:" + orders.getOrderId());
            List<OrderDetail> detailList = orders.getOrderDetailList();
            try{Thread.sleep(2000);}catch (Exception e){e.printStackTrace();}
            for (OrderDetail detail: detailList) {
                System.out.println(detail.getGoods());
            }
        }
    }
    @Test
    public void testUseGoods() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());
        try{Thread.sleep(5000);}catch (Exception e){e.printStackTrace();}
        System.out.println("---------");
        for (Orders orders: user.getOrdersList()) {
            for (OrderDetail orderDetail: orders.getOrderDetailList()) {
                System.out.println(orderDetail.getGoods());
            }
        }
    }
    @Test
    public void testOrderUser() throws Exception{
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrdersId(1022);
        System.out.println(orders.getOrderId()+"-"+orders.getOrderDate());
        try {
            Thread.sleep(10000);}catch (Exception e){e.printStackTrace();
        }
        System.out.println(orders.getUser());
    }
    @Test
    public void testWs() throws Exception{
        Worker worker = new Worker();
        worker.setWid("1540669829059465216");
        worker.setWname("大律师");
        worker.setAge(45);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.updateWorkerByCondition(worker);
        System.out.println(row);

    }

    @Test
    public void test4() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = (User) mapper.queryUserOrders(702);
        System.out.println(user);

        for (Orders orders : user.getOrdersList()){
            System.out.println(orders + "!!!");

        }

    }
    @Test
    public void test3() throws Exception{
        Goods goods = new Goods();
        goods.setGoodsName("%C%");
        goods.setShopPrice(18.0);
        goods.setGoodsStock(900);

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.queryByCondition(goods);
        goodsList.forEach(System.out::println);
    }
    @Test
    public void test2() throws Exception{
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(9003);
        ids.add(9008);
        ids.add(9009);

        List<Goods> goods = goodsMapper.queryByIds(ids);
        for (Goods good : goods){
            System.out.println(good);
        }

        Goods goods1 = goodsMapper.queryByIdAno(9001);
        System.out.println(goods1);
    }
    @Test
    public void testVo() throws Exception{
        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryByRoleAndAddress(vo);

        users.forEach(System.out::println);

    }
    @Test
    public void testMap() throws Exception{
        HashMap<String,Object> map = new HashMap<>();
        map.put("roleCode","plain");
        map.put("address","济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryByMap(map);

        users.forEach(System.out::println);

    }
    @Test
    public void testTea() throws Exception{
        Integer tid = 1009;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryByID(tid);
        System.out.println(teacher);

    }
    @Test
    public void testUser() throws Exception{
        int uid = 702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);

    }
    @Test
    public void testTabName() throws Exception{
        String tabName = "goods";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName + "=" + counts);

    }
    @Test
    public void testMapperLike2() throws Exception{
        String name = "X";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName2(name);
        for (Goods good : goods){
            System.out.println(good);
        }

    }
    @Test
    public void testMapperLike() throws Exception{
        String name = "%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        for (Goods good : goods){
            System.out.println(good);
        }

    }
    @Test
    public void testMapperGoods() throws Exception{
        Goods goods = new Goods();
        goods.setGoodsName("品胜 type数据线");
        goods.setShopPrice(15.88);

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        int i = goodsMapper.addGoods(goods);
        System.out.println(goods);

    }
    @Test
    public void testMapper3() throws Exception{
        Worker worker = new Worker();
        worker.setWname("警察");
        worker.setAge(30);
        worker.setSex("女");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int i = workerMapper.insertworker2(worker);
        System.out.println(i+"-------" + worker);
    }
    @Test
    public void testMapper2() throws Exception{
        Worker worker = new Worker();
        //主键 整数自增 字符串uid，雪花算法
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("小龙女");
        worker.setAge(30);
        worker.setSex("女");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);
    }
    @Test
    public void testMapper1() throws Exception{
        //获取mapper接口（dao）的动态代理实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workers = workerMapper.selectByAge(50);
        workers.forEach(System.out::println);
    }


    @Before
    public void before() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
    }
    @After
    public void After() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testUpDate() throws Exception{
        //演示一个修改
        Worker worker = new Worker();
        worker.setAge(500);
        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        int row = sqlSession.update("Worker.updateAgeByID",worker);
        System.out.println(row);


    }

}
